package com.peaker.advertisingpro.view;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Handler;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.TextView;

/**
 * @ 作者 CODE_LYF
 * @ 时间   2021/3/22.
 * 代码人生
 */
@SuppressLint("AppCompatCustomView")
public class CustomTextView extends TextView {
    public CustomTextView(Context context) {
        super(context);
    }
    public CustomTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    public CustomTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                Log.i("CustomTextView", "Before X:" + getX() + " Y:" + getY()
                        + " Left:" + getLeft() + " Top:" + getTop()
                        + " Right:" + getRight() + " Bottom:" + getBottom());

                // layout
//                layout(getLeft() + 100, getTop() + 100, getRight() + 100, getBottom() + 100);

                // offsetLeftAndRight、offsetTopAndBottom
//                offsetLeftAndRight(100);
//                offsetTopAndBottom(100);

                // setLayoutParams
            /*    ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) getLayoutParams();
                lp.leftMargin = getLeft() + 100;
                lp.topMargin = getTop() + 100;
                setLayoutParams(lp);*/

                // scrollTo scrollBy
//                scrollTo(0, (int) (getY()+100));
//                ((View) getParent()).scrollTo(-100, -100);
//                ((View) getParent()).scrollBy(-100, -100);

                // 属性动画
                AnimatorSet animatorSet = new AnimatorSet();
                animatorSet.playTogether(
                        ObjectAnimator.ofFloat(this, "translationX", 0),
                        ObjectAnimator.ofFloat(this, "translationY", getY()+100)
                );
                animatorSet.start();

                // 位移动画
//                TranslateAnimation anim = new TranslateAnimation(0, 100, 0, 100);
//                anim.setFillAfter(true);
//                startAnimation(anim);

                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        Log.i("CustomTextView", "After X:" + getX() + " Y:" + getY()
                                + " Left:" + getLeft() + " Top:" + getTop()                                + " Right:" + getRight() + " Bottom:" + getBottom());
                    }
                }, 3000);
                break;
        }
        return false;
    }}